VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ObjPolygon"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit

Private num_points As Integer
Private y() As Single
Private x() As Single

Public Drawn As Boolean
' ************************************************
' Return True if this polygon contains the
' indicated point.
' ************************************************
Function Contains(x As Single, y As Single) As Boolean
Dim xmin As Single
Dim ymin As Single
Dim xmax As Single
Dim ymax As Single

    Bound xmin, ymin, xmax, ymax
    Contains = (x >= xmin And x <= xmax And _
                y >= ymin And y <= ymax)
End Function

Function ObjectType() As String
    ObjectType = "POLYGON"
End Function

' ************************************************
' Set the coordinates for a point.
' ************************************************
Sub SetPoint(Index As Integer, x1 As Single, y1 As Single)
    If Index < 1 Or Index > num_points Then Exit Sub
    x(Index) = x1
    y(Index) = y1
End Sub

' ************************************************
' Apply a transformation matrix to the object.
' ************************************************
Sub Transform(M() As Single)
Dim i As Integer

    For i = 1 To num_points
        m2PointMultiply x(i), y(i), M
    Next i
End Sub

' ************************************************
' Apply a nonlinear transformation.
' ************************************************
Sub Distort(d As Object)
Dim i As Integer

    For i = 1 To num_points
        d.Distort x(i), y(i)
    Next i
End Sub
' ************************************************
' Set the number of points and redimension the
' point arrays.
' ************************************************
Property Let NumPoints(n As Integer)
    If n < 1 Then
        Erase x
        Erase y
        num_points = 0
        Exit Property
    End If
    
    num_points = n
    ReDim x(1 To num_points)
    ReDim y(1 To num_points)
End Property
' ************************************************
' Compute the world coordinate bounds for the
' polygon.
' ************************************************
Sub Bound(xmin As Single, ymin As Single, xmax As Single, ymax As Single)
Dim i As Integer

    If num_points = 0 Then
        xmin = 0
        xmax = 0
        ymin = 0
        ymax = 0
        Exit Sub
    End If
    
    xmin = x(1)
    xmax = x(1)
    ymin = y(1)
    ymax = y(1)
    For i = 2 To num_points
        If x(i) < xmin Then xmin = x(i)
        If y(i) < ymin Then ymin = y(i)
        If x(i) > xmax Then xmax = x(i)
        If y(i) > ymax Then ymax = y(i)
    Next i
End Sub

' ************************************************
' Write a polygon to a file using Write.
' Begin with "POLYGON" to identify this object.
' ************************************************
Sub FileWrite(filenum As Integer)
Dim i As Integer

    Write #filenum, "POLYGON", num_points
    For i = 1 To num_points
        Write #filenum, x(i), y(i)
    Next i
End Sub
' ************************************************
' Draw the polygon on a Form, Printer, or
' PictureBox.
' ************************************************
Sub Draw(canvas As Object)
Dim i As Integer

    ' Don't draw if there are no points.
    If num_points < 1 Then Exit Sub
    
    ' Don't draw if it has been drawn before.
    If Drawn Then Exit Sub
    Drawn = True
    NumDraws = NumDraws + 1
    
    canvas.CurrentX = x(1)
    canvas.CurrentY = y(1)
    For i = 2 To num_points
        canvas.Line -(x(i), y(i))
    Next i
End Sub

' ************************************************
' Read a polygon from a file using Input.
' Assume the "POLYGON" label has already been read.
' ************************************************
Sub FileInput(filenum As Integer)
Dim i As Integer

    Input #filenum, num_points
    If num_points < 1 Then Exit Sub
    ReDim x(1 To num_points)
    ReDim y(1 To num_points)
    For i = 1 To num_points
        Input #filenum, x(i), y(i)
    Next i
End Sub


